package nongchang.kuaijiecaifu.com.ncapp_android.util;

import android.os.CountDownTimer;
import android.widget.TextView;

/**
 * Created by ezy on 15/11/10.
 */
public class Helpers {
    public static String way(int value) {
        switch (value) {
            case 1:
                return "PC端";
            case 2:
                return "移动端";
            case 3:
                return "自动投标";
        }
        return "手动投标";
    }

    public static String rateFormat(float rate, float reward) {
        return reward < 0.0001 ? String.format("%-1.0f", rate) : String.format("%-1.0f+%-1.0f", rate, reward);
    }

    public static int duetimeValue(int duetime) {
        return duetime < 30 ? duetime : duetime / 30;
    }

    public static String duetimeUnit(int duetime) {
        return duetime < 30 ? "天" : "个月";
    }

    public static String duetimeFormat(int duetime) {
        return duetime < 30 ? (duetime + "天") : (duetime / 30 + "个月");
    }

    public static String amountValue(float value) {
        return "" + (int) (value >= 100000 ? value / 10000 : value);
    }

    public static String amountValue(int value) {
        return "" + (value >= 100000 ? value / 10000 : (value >= 1000) ? addComma(String.valueOf(value)) : value);
    }

    public static String amountUnit(float value) {
        return value >= 100000 ? "万元" : "元";
    }

    public static String amountUnit(int value) {
        return value >= 100000 ? "万元" : "元";
    }

    public static String amountFormat(float value) {
        return value >= 100000 ? ((int) value / 10000 + "万元") : ((int) value + "元");
    }

    public static String amountFormat(int value) {
        return value >= 100000 ? (value / 10000 + "万元") : ((value >= 1000) ? addComma(String.valueOf(value))+"元" : value+"元");
    }


    public static void countdown(TextView btn) {
        countdown(btn, 60);
    }

    public static void countdown(TextView btn, int seconds) {
        new CountDownTimer(seconds * 1000, 1000) {
            @Override
            public void onTick(long s) {
                btn.setText((int) (s / 1000) + "秒后重新获取");
                btn.setEnabled(false);
            }

            @Override
            public void onFinish() {
                btn.setText("发送验证码");
                btn.setEnabled(true);
            }
        }.start();
    }

    /**
     * &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
     * 将每三个数字加上逗号处理（通常使用金额方面的编辑）
     *
     * @param str 无逗号的数字
     *            <a href="http://home.51cto.com/index.php?s=/space/34010" target="_blank">@return</a> 加上逗号的数字
     */
    public static String addComma(String str) {
// 将传进数字反转
        String reverseStr = new StringBuilder(str).reverse().toString();
        String strTemp = "";
        for (int i = 0; i < reverseStr.length(); i++) {
            if (i * 3 + 3 > reverseStr.length()) {
                strTemp += reverseStr.substring(i * 3, reverseStr.length());
                break;
            }
            strTemp += reverseStr.substring(i * 3, i * 3 + 3) + ",";
        }
// 将[789,456,] 中最后一个[,]去除
        if (strTemp.endsWith(",")) {
            strTemp = strTemp.substring(0, strTemp.length() - 1);
        }
// 将数字重新反转
        String resultStr = new StringBuilder(strTemp).reverse().toString();
        return resultStr;
    }
}
